import java.util.ArrayList;
import java.util.Hashtable;

public class Action {

	int name;
	ArrayList<ActionArgument> arguments = new ArrayList<ActionArgument>();
	State pre;
	State post;

	public Action copy() {
		Action wyn = new Action();
		wyn.name = name;
		wyn.pre = pre.copy();
		wyn.post = post.copy();
		for (ActionArgument a : arguments) {
			wyn.arguments.add(a.copy());
		}
		return wyn;
	}

	public void populateVariables() {
		Hashtable<Integer, Variable> newPre = new Hashtable<Integer, Variable>();
		for (int x : pre.variables.keySet()) {
			Variable v = pre.variables.get(x);
			for (int i = 0; i < v.values.size(); i++) {
				String s = v.values.get(i);
				String value = getValue(s);
				if (value == null) {
					value = s;
				}
				v.values.set(i, value);
			}
			newPre.put(v.hashCode(), v);
		}
		pre.variables = newPre;
		Hashtable<Integer, Variable> newPost = new Hashtable<Integer, Variable>();
		for (int x : post.variables.keySet()) {
			Variable v = post.variables.get(x);
			for (int i = 0; i < v.values.size(); i++) {
				String s = v.values.get(i);
				String value = getValue(s);
				if (value == null) {
					value = s;
				}
				v.values.set(i, value);
			}
			newPost.put(v.hashCode(), v);
		}
		post.variables = newPost;
	}

	private String getValue(String s) {
		String wyn = null;
		for (ActionArgument a : arguments) {
			if (a.nazwa.equals(s)) {
				return a.value;
			}
		}
		return wyn;
	}

	public String toString() {
		String wyn = Parser.stringi.get(name);
		for (ActionArgument a : arguments) {
			wyn += " " + a;
		}
		return wyn;
	}
	
	public int hashCode(){
		return toString().hashCode();
	}
}
